//
//  LBPageSmoothView.h
//  TEXT
//
//  Created by mac on 2024/8/11.
//  Copyright © 2024 刘博. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, LBPageSmoothHoverType) {
    LBPageSmoothHoverTypeNone   = 0,    // 未悬停
    LBPageSmoothHoverTypeTop    = 1,    // 顶部悬停
};

@class LBPageSmoothView;

@protocol LBPageSmoothListViewDelegate <NSObject>

/// 返回listView，如果是vc就返回vc.view,如果是自定义view，就返回view本身
- (UIView *)listView;

/// 返回vc或view内部持有的UIScrollView或UITableView或UICollectionView
- (UIScrollView *)listScrollView;

@optional
- (void)listViewWillAppear;
- (void)listViewDidAppear;
- (void)listViewWillDisappear;
- (void)listViewDidDisappear;

- (BOOL)hasSubSegment;

/// 当contentSize改变且不足一屏时，是否重置scrollView的位置，默认YES
- (BOOL)listScrollViewShouldReset;

- (void)updateOtherSublistScrollViewContentWithCurrentListScroll:(UIScrollView *)scrollView contentInset:(UIEdgeInsets)contentInset;

- (void)updateOtherSublistScrollViewContentOffsetWithCurrentListScroll:(UIScrollView *)scrollView contentOffset:(CGPoint)contentOffset;

- (void)removeObsever:(NSObject *)observer keyPath:(NSString *)keyPath scrollView:(UIScrollView *)scrollView;

- (void)addObserver:(NSObject *)observer keyPath:(NSString *)keyPaht scrollView:(UIScrollView *)scrollView;

- (void)configScrollsTopWithScrollView:(UIScrollView *)scrollView;

@end

@protocol LBPageSmoothViewDataSource <NSObject>

/// 返回页面的headerView视图
/// @param smoothView smoothView
- (UIView *)headerViewInSmoothView:(LBPageSmoothView *)smoothView;

/// 返回需要悬浮的分段视图
/// @param smoothView smoothView
- (UIView *)segmentedViewInSmoothView:(LBPageSmoothView *)smoothView;

/// 返回列表个数
/// @param smoothView smoothView
- (NSInteger)numberOfListsInSmoothView:(LBPageSmoothView *)smoothView;

/// 根据index初始化一个列表实例，列表需实现`GKPageSmoothListViewDelegate`代理
/// @param smoothView smoothView
/// @param index 列表索引
- (id<LBPageSmoothListViewDelegate>)smoothView:(LBPageSmoothView *)smoothView initListAtIndex:(NSInteger)index;

- (BOOL)hasSubSegmentAtIndex:(NSInteger)index;

@end

@protocol LBPageSmoothViewDelegate <NSObject>

@optional
/// 列表容器滑动代理
/// @param smoothView smoothView
/// @param scrollView containerScrollView
- (void)smoothView:(LBPageSmoothView *)smoothView scrollViewDidScroll:(UIScrollView *)scrollView;

/// 当前列表滑动代理
/// @param smoothView smoothView
/// @param scrollView 当前的列表scrollView
/// @param contentOffset 转换后的contentOffset
- (void)smoothView:(LBPageSmoothView *)smoothView listScrollViewDidScroll:(UIScrollView *)scrollView contentOffset:(CGPoint)contentOffset;

@end

@interface LBPageSmoothView : UIView

/// 代理
@property (nonatomic, weak) id<LBPageSmoothViewDelegate> delegate;

// 当前已经加载过的可用的列表字典，key是index值，value是对应列表
@property (nonatomic, strong, readonly) NSDictionary <NSNumber *, id<LBPageSmoothListViewDelegate>> *listDict;
@property (nonatomic, strong, readonly) UICollectionView *listCollectionView;

/// 默认索引
@property (nonatomic, assign) NSInteger defaultSelectedIndex;

/// 当前索引
@property (nonatomic, assign, readonly) NSInteger currentIndex;

/// 当前列表
@property (nonatomic, weak, readonly) UIScrollView *currentListScrollView;

/// 吸顶临界高度（默认为0）
@property (nonatomic, assign) CGFloat ceilPointHeight;

/// 是否内部控制指示器的显示与隐藏（默认为NO）
@property (nonatomic, assign, getter=isControlVerticalIndicator) BOOL controlVerticalIndicator;

/// 是否撑起scrollView，默认NO
/// 如果设置为YES则当scrollView的contentSize不足时会修改scrollView的contentSize使其能够滑动到悬浮状态
@property (nonatomic, assign, getter=isHoldUpScrollView) BOOL holdUpScrollView;

/// smoothView悬停类型
@property (nonatomic, assign, readonly) LBPageSmoothHoverType hoverType;

/// header容器的高度
@property (nonatomic, assign, readonly) CGFloat headerContainerHeight;

- (instancetype)initWithDataSource:(id<LBPageSmoothViewDataSource>)dataSource NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;

/**
 刷新headerView，headerView高度改变时调用
 */
- (void)refreshHeaderView;

/**
 刷新segmentedView，segmentedView高度改变时调用
 */
- (void)refreshSegmentedView;

/**
 刷新数据，刷新后pageView才能显示出来
 注意：如果需要动态改变headerView的高度，请在refreshHeaderView后在调用reloadData方法
 */
- (void)reloadData;

/**
 滑动到原点，可用于在吸顶状态下，点击返回按钮，回到原始状态
 */
- (void)scrollToOriginalPoint;
- (void)scrollToOriginalPointAnimated:(BOOL)animated;

/**
 滑动到临界点，可用于当headerView较长情况下，直接跳到临界点状态
 */
- (void)scrollToCriticalPoint;
- (void)scrollToCriticalPointAnimated:(BOOL)animated;

- (void)horizontalScrollDidEndAtIndex:(NSInteger)index;
- (void)selectIndex:(NSInteger)index;

- (void)updateUIWhenSubContainerScrollViewDidScroll;

- (void)updateCurrentListViewWhenContainerDidEndDecelerating;

- (void)removeObserVerForListView:(UIScrollView *)scrollView;

- (void)addObserverForListView:(UIScrollView *)scrollView;

@end

NS_ASSUME_NONNULL_END
